package com.csvreader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataSummary {
	// private String datadir;
	File datafile;

	DataSummary(String datadir) {
		datafile = new File(datadir);
	}
	/**
	 * @Desription 根据时间前缀 ，统计记录信息
	 * @param date_prefix 比如 “20170907”
	 * @param minute 文件
	 * @return
	 */
	public List<MinuteSummary> minuteCalculation(String date_prefix,DirectoryType directoryType) {
		
		String str_yyyy=date_prefix.substring(0,4);
		List<MinuteSummary> result = new ArrayList<MinuteSummary>();
		
		File minuteFile = new File(datafile.getPath() + "\\"+directoryType.folderName);
		File[] files = minuteFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				File childfile=files[i];
				if(childfile.isDirectory()) {//D:\boochy\data\minute\XSHE
					File[] childcodefile=childfile.listFiles();
					for (int j = 0; j < childcodefile.length; j++) {
						if (childcodefile[j].isDirectory()) {//D:\boochy\data\minute\XSHE\000688
							
							File[] csvfile=childcodefile[j].listFiles();
							for (int k = 0; k < csvfile.length; k++) {
								if(csvfile[k].isFile()) {
									if(csvfile[k].getName().startsWith(str_yyyy)) {
										MinuteSummary minutesummary= new MinuteSummary();
										minutesummary.setTrock_code(childcodefile[j].getName());
										minutesummary.setFileSummary(fileCalculation(date_prefix,csvfile[k]));
										result.add(minutesummary);
									}
									
								}
							}
							
							
						}
					}
				}
			}
		}
		return result;
	}

	/**
	 * @Desription 根据时间前缀 ，统计记录信息
	 * @param date_prefix 比如 “20170907”
	 * @param file csv 文件
	 * @return
	 */
	private FileSummary fileCalculation(String date_prefix, File file) {
		FileSummary result = new FileSummary();
		List<Integer> list = new ArrayList<Integer>();//记录记录的hashcode
		Integer record_num=0;
		Integer h=0;
		
		try {
			// 创建CSV读对象
			CsvReader csvReader = new CsvReader(file.getAbsolutePath());

			// 读表头
			csvReader.readHeaders();
			while (csvReader.readRecord()) {
				if (csvReader.get("min_time").startsWith(date_prefix)) {
					list.add(csvReader.getRawRecord().hashCode());
					record_num++;
					//h=h|csvReader.getRawRecord().hashCode();
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		result.setHashcode(list.hashCode());
		
		//result.setHashcode(h);
		result.setRecord_num(record_num);
		return result;
	}

}
